package com.mellarius.activities;

import java.util.Calendar;

import com.mellarius.R;
import com.mellarius.fragments.FragmentRepository;
import com.mellarius.fragments.FragmentRepository.MainApiariesFragment;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.Toast;


public class BatchOperation extends FragmentActivity implements OnMenuItemClickListener {

	final Context context = this;
	private FragmentManager fragManager;
	private FragmentTransaction transaction;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		fragManager = getSupportFragmentManager();
		setContentView(R.layout.activity_batch_operation);
		transaction = fragManager.beginTransaction();
		transaction.replace(R.id.slecthivesFrame, new FragmentRepository.HiveSelectorFragment());
		transaction.commit();

	}

	protected void onPause() {
		
		super.onPause();
//		Intent intent = new Intent(this, GestorActividade.class);
//		startActivity(intent);
		finish();
	}
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.action_Apiario:
		Toast.makeText(this, MainApiariesFragment.getApiary().getIdentificacao(), Toast.LENGTH_SHORT).show();
		break;
		default:
		break;
		}
		return super.onOptionsItemSelected(item);
	}

	/** instantiates the activity BatchOperation */
	public void openAlimentationDialog(View view) {
		PopupMenu popupMenu = new PopupMenu(BatchOperation.this, view);
		popupMenu.setOnMenuItemClickListener(BatchOperation.this);
		popupMenu.getMenu().add(2, 0, 1, R.string.batchOperation_colocarAlimentacao);
		popupMenu.show();

	}

	public void openTraramentoDialog(View view) {
		PopupMenu popupMenu = new PopupMenu(BatchOperation.this, view);
		popupMenu.setOnMenuItemClickListener(BatchOperation.this);
		popupMenu.getMenu().add(1, 0, 1, R.string.batchOperation_colocartratamento);
		popupMenu.getMenu().add(1, 1, 1, R.string.batchOperation_retirartratamento);
		popupMenu.show();

	}

	public void openColheitaDialog(View view) {
		PopupMenu popupMenu = new PopupMenu(BatchOperation.this, view);
		popupMenu.setOnMenuItemClickListener(BatchOperation.this);
		popupMenu.getMenu().add(0, 0, 1, R.string.batchOperation_registarColheita);
		popupMenu.getMenu().add(0, 1, 1, R.string.batchOperation_reporCeras);
		popupMenu.show();

	}

	@Override
	public boolean onMenuItemClick(MenuItem arg0) {
		// TODO Auto-generated method stub
		if (arg0.getGroupId() == 0 && arg0.getItemId() == 0) {
			transaction = fragManager.beginTransaction();
			transaction.replace(R.id.operationsFrame, new FragmentRepository.BatchOpNoteHarvest());
			transaction.addToBackStack(null);
			transaction.commit();
		}
		if (arg0.getGroupId() == 0 && arg0.getItemId() == 1) {
			transaction = fragManager.beginTransaction();
			transaction.replace(R.id.operationsFrame,new FragmentRepository.BatchOpReplaceSuper());
			transaction.addToBackStack(null);
			transaction.commit();
		}
		if (arg0.getGroupId() == 1 && arg0.getItemId() == 0) {
			transaction = fragManager.beginTransaction();
			transaction.replace(R.id.operationsFrame, new FragmentRepository.BatchOpPutTreatment());
			transaction.addToBackStack(null);
			transaction.commit();

		}
		if (arg0.getGroupId() == 1 && arg0.getItemId() == 1) {
			transaction = fragManager.beginTransaction();
			transaction.replace(R.id.operationsFrame, new FragmentRepository.BatchOpDeleteTreatment());
			transaction.addToBackStack(null);
			transaction.commit();
		}
		if (arg0.getGroupId() == 2 && arg0.getItemId() == 0) {
			transaction = fragManager.beginTransaction();
			transaction.replace(R.id.operationsFrame, new FragmentRepository.BatchOpCreateAlimentation());
			transaction.addToBackStack(null);
			transaction.commit();
		}

		return false;
	}

	public void datePutTreatement(int year, int month, int day) {
		EditText mEdit;

			mEdit = (EditText)findViewById(R.id.PutTreatmentData);
			mEdit.setText(day+"/"+month+"/"+year);	
	}

	public void selectDatePutTreatement(View view) {
		DialogFragment newFragment = new SelectDateTratamento();
		newFragment.show(getFragmentManager(), "DatePicker");
	}
	public void selectDateRemoveTreatement(View view) {
		DialogFragment newFragment = new SelectDateTratamento();
		newFragment.show(getFragmentManager(), "DatePicker");
	}


	public void setMelgueiras() {

	}

	@SuppressLint("ValidFragment")
	public class SelectDateTratamento extends DialogFragment implements DatePickerDialog.OnDateSetListener {

		public SelectDateTratamento() {

		}

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			Calendar calendar = Calendar.getInstance();
			int yy = calendar.get(Calendar.YEAR);
			int mm = calendar.get(Calendar.MONTH);
			int dd = calendar.get(Calendar.DAY_OF_MONTH) + 1;
			return new DatePickerDialog(getActivity(), this, yy, mm, dd);
		}

		@Override
		public void onDateSet(DatePicker view, int yy, int mm, int dd) {
			datePutTreatement(yy, mm + 1, dd);
		}

	}


}
